<template>
  <div class="course">
    <div class="course-container">
      <div class="course-header">
        <div class="title">{{ datas.form.title }}</div>
        <div class="more">更多<i class="el-icon-arrow-right right"></i></div>
      </div>

      <div class="course-body">
        <!-- 最近直播 -->
        <template v-if="datas.form.source === 'lately_live'">
          <!-- 宫格 -->
          <div
            v-if="datas.form.arrange_type == 1"
            class="course-body-live grid"
            :class="{
              'grid-columns-2': datas.form.column == 2,
              'grid-columns-3': datas.form.column == 3,
              'grid-columns-4': datas.form.column == 4
            }"
          >
            <div v-for="item in datas.mock_source" :key="item" class="item">
              <div class="image">
                <el-image :src="item.cover_url">
                  <div slot="error" class="image-error">
                    <i class="el-icon-s-custom image-error__icon" />
                  </div>
                </el-image>
                <div v-if="datas.form.column != 2" class="time">
                  1月1日 00:00
                </div>
                <div v-else class="tag">
                  <img
                    class="icon"
                    src="@/assets/images/template/play.png"
                  />直播中
                </div>
              </div>
              <div class="content">
                <div class="title">
                  <!-- 活动图标 -->
                  <img
                    v-if="datas.form.source === 'event'"
                    src="@/assets/images/template/pc/course_discount_icon.png"
                  />
                  <span class="name">{{ item.title }}</span>
                </div>
                <div v-if="datas.form.column == 2" class="time">
                  04月21日 19:00-21:00
                </div>
                <div class="bottom">
                  <span class="text">讲师：李毅</span>
                  <span class="line"></span>
                  <span class="text">{{ item.num }}人报名</span>
                  <div v-if="datas.form.source !== 'free'" class="price">¥{{ item.price }}</div>
                  <div v-else class="free">免费</div>
                </div>
              </div>
            </div>
          </div>

          <!-- 滚动 -->
          <div
            v-else-if="datas.form.arrange_type == 2"
            class="course-body-live scroll"
          >
            <swiper :options="liveSwiperOption">
              <swiper-slide
                v-for="(item,index) in datas.mock_source"
                :key="item"
              >
                <div class="header">
                  <div class="title">今天</div>
                  <div class="line">
                    <span class="num">19:00-21:00</span>
                  </div>
                </div>
                <div class="item">
                  <div class="image">
                    <el-image :src="item.cover_url">
                      <div slot="error" class="image-error">
                        <i class="el-icon-s-custom image-error__icon" />
                      </div>
                    </el-image>
                    <div class="tag">
                      <img
                        class="icon"
                        src="@/assets/images/template/play.png"
                      />直播中
                    </div>
                  </div>
                  <div class="content">
                    <div class="title">
                      <!-- 活动图标 -->
                      <img
                        v-if="datas.form.source === 'event'"
                        :src="eventIcon[index % 3]"
                      />
                      <span class="name">{{ item.title }}</span>
                    </div>
                    <div class="time">04月23日 14:00-16:00</div>
                    <div class="bottom">
                      <span class="text">讲师：李毅</span>
                      <span class="line"></span>
                      <span class="text">{{ item.num }}人报名</span>
                      <div v-if="datas.form.source !== 'free'" class="price">¥{{ item.price }}</div>
                      <div v-else class="free">免费</div>
                    </div>
                  </div>
                </div>
              </swiper-slide>
            </swiper>
            <div class="swiper-button-prev swiper-button-white"></div>
            <div class="swiper-button-next swiper-button-white"></div>
          </div>
        </template>

        <!-- 其他课程 -->
        <template v-else>
          <!-- 宫格 -->
          <div
            v-if="datas.form.arrange_type == 1"
            class="course-body-list grid"
            :class="{
              'grid-columns-2': datas.form.column == 2,
              'grid-columns-3': datas.form.column == 3,
              'grid-columns-4': datas.form.column == 4
            }"
          >
            <div
              v-for="(item,index) in datas.mock_source"
              :key="item"
              class="item"
            >
              <div class="image">
                <el-image :src="item.cover_url">
                  <div slot="error" class="image-error">
                    <img
                      class="image-error__icon"
                      src="@/assets/images/template/placeholder.png"
                    />
                  </div>
                </el-image>

                <!-- 活动图标 -->
                <img
                  v-if="datas.form.source === 'event'"
                  class="activity-icon"
                  :src="eventIcon[index % 3]"
                />

                <!-- tag -->
                <img
                  class="tag-icon"
                  :src="courseTypeIcon[item.course_type]"
                />
              </div>
              <div class="desc">
                <div class="title">{{ item.title }}</div>
                <div class="bottom">
                  <div class="number">{{ item.num }}人报名</div>
                  <div v-if="datas.form.source !== 'free'" class="price">¥{{ item.price }}</div>
                  <div v-else class="free">免费</div>
                </div>
              </div>
            </div>
          </div>

          <!-- 滚动 -->
          <div
            v-else-if="datas.form.arrange_type == 2"
            class="course-body-list scroll"
          >
            <swiper :options="courseSwiperOption">
              <swiper-slide
                v-for="(item,index) in datas.mock_source"
                :key="item"
              >
                <div class="item">
                  <div class="image">
                    <el-image :src="item.cover_url">
                      <div slot="error" class="image-error">
                        <img
                          class="image-error__icon"
                          src="@/assets/images/template/placeholder.png"
                        />
                      </div>
                    </el-image>

                    <!-- 活动图标 -->
                    <img
                      v-if="datas.form.source === 'event'"
                      class="activity-icon"
                      :src="eventIcon[index % 3]"
                    />

                    <!-- tag -->
                    <img
                      class="tag-icon"
                      :src="courseTypeIcon[item.course_type]"
                    />
                  </div>
                  <div class="desc">
                    <div class="title">{{ item.title }}</div>
                    <div class="bottom">
                      <div class="number">{{ item.num }}人报名</div>
                      <div v-if="datas.form.source !== 'free'" class="price">¥{{ item.price }}</div>
                      <div v-else class="free">免费</div>
                    </div>
                  </div>
                </div>
              </swiper-slide>
            </swiper>
            <div class="swiper-button-prev swiper-button-white"></div>
            <div class="swiper-button-next swiper-button-white"></div>
          </div>
        </template>
      </div>
    </div>

    <!-- 删除组件 -->
    <slot name="deles" />
  </div>
</template>

<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
  name: 'Course',
  components: {
    swiper,
    swiperSlide
  },
  props: {
    datas: {
      type: Object,
      default: () => {}
    },
    isDroping: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      courseSwiperOption: {
        slidesPerView: 4,
        spaceBetween: 0,
        loop: false,
        loopAdditionalSlides: 0,
        lazy: true,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      },
      liveSwiperOption: {
        slidesPerView: 2,
        spaceBetween: 0,
        loop: false,
        loopAdditionalSlides: 0,
        lazy: true,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      },
      sourceList: [],
      courseTypeIcon: {
        1: require('@/assets/images/template/pc/course_video_icon.png'),
        2: require('@/assets/images/template/pc/course_live_icon.png'),
        4: require('@/assets/images/template/pc/course_class_icon.png')
      },
      eventIcon: {
        0: require('@/assets/images/template/pc/course_kanjia_icon.png'),
        1: require('@/assets/images/template/pc/course_discount_icon.png'),
        2: require('@/assets/images/template/pc/course_miaosha_icon.png')
      }
    }
  },
  watch: {
    'datas.form.source_index': {
      handler() {
        this.loadBatchInfo()
      }
    }
  },
  created() {
    if (this.isDroping === false) {
      this.loadBatchInfo()
    }
  },
  methods: {
    loadBatchInfo() {
      let sourceList = []
      if (this.datas.form.source === 'custom') {
        const ids = this.datas.form.source_list.map(item => item.value)
        if (ids.length > 0) {
          this.$http.get(
            '/course/admin/batch/info',
            {
              ids: ids
            },
            ({ data }) => {
              this.datas.form.source_list.forEach(item => {
                const info = data.find(i => i.id === item.value)
                sourceList.push({
                  id: info.id,
                  title: info.title,
                  course_type: info.course_type,
                  num: info.sale_count_market + info.sale_count,
                  price: info.price,
                  cover_url: info.cover_url
                })
              })
            },
            _ => {}
          )
        }
      } else {
        sourceList = this.getMockData()
      }

      // this.sourceList = sourceList
      this.$set(this.datas, 'mock_source', sourceList)
    },
    // 课程模拟数据
    getMockData() {
      const mockData = []
      const videoCourse = {
        title: '示例点播课程',
        course_type: 1,
        num: 123,
        price: 99.99,
        cover_url: ''
      }
      const liveCourse = {
        title: '示例直播课程',
        course_type: 2,
        num: 123,
        price: 99.99,
        cover_url: ''
      }
      const classesCourse = {
        title: '示例班级课程',
        course_type: 4,
        num: 123,
        price: 99.99,
        cover_url: ''
      }
      switch (this.datas.form.course_type) {
        case 2:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...liveCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
        case 4:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...classesCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
        default:
          for (let i = 0; i < this.datas.form.number; i++) {
            const item = { ...videoCourse }
            item.cover_url = require('@/assets/images/template/course_' +
              ((i % 4) + 1) +
              '.png')
            mockData.push(item)
          }
          break
      }

      return mockData
    }
  }
}
</script>

<style scoped lang="scss">
.course {
  position: relative;
  padding: 15px 0;
  &-container {
    width: 834px;
    margin: 0 auto;
    background: #ffffff;
  }
  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 62px;
    padding: 0 15px;
    .title {
      font-size: 17px;
      font-weight: 500;
      color: #303133;
      line-height: 24px;
    }
    .more {
      font-size: 12px;
      font-weight: 400;
      color: #888888;
      line-height: 16px;
    }
  }
  &-body {
    padding: 0 15px 20px;
    &-list {
      .item {
        min-width: 0;
        .image {
          position: relative;
          .el-image {
            width: 100%;
            height: 100%;
            ::v-deep.image-error {
              width: 100%;
              height: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              width: 100%;
              height: 100%;
              background-color: #f1f1f1;
              border-radius: 6px;
              .image-error__icon {
                width: 40px;
                height: 28px;
              }
            }
          }
          .activity-icon {
            position: absolute;
            right: 0;
            bottom: 0;
          }
          .tag-icon {
            position: absolute;
            left: 0;
            top: 0;
          }
        }
        .title {
          font-size: 14px;
          font-weight: 500;
          color: #333333;
          line-height: 20px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .bottom {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        .number {
          font-size: 12px;
          font-weight: 400;
          color: #888888;
        }
        .price {
          font-size: 14px;
          font-weight: 500;
          color: #f54030;
        }
        .free {
          font-size: 14px;
          font-weight: 500;
          color: #67c23a;
        }
      }

      &.grid {
        display: grid;
        grid-gap: 14px 10px;

        &.grid-columns-2 {
          grid-template-columns: repeat(2, 1fr);
          .item {
            background: rgba(48, 49, 51, 0.02);
            border-radius: 6px;
            padding: 10px;
            display: flex;
            .image {
              width: 191px;
              height: 105px;
            }
            .desc {
              flex: 1;
              min-width: 0;
              margin-left: 20px;
              padding: 10px 0;
            }
          }
        }
        &.grid-columns-3 {
          grid-template-columns: repeat(3, 1fr);
          .item {
            .image {
              height: 140px;
            }
            .desc,
            .bottom {
              margin-top: 12px;
            }
          }
        }
        &.grid-columns-4 {
          grid-template-columns: repeat(4, 1fr);
          .item {
            .image {
              height: 105px;
            }
            .desc,
            .bottom {
              margin-top: 12px;
            }
          }
        }
      }
      &.scroll {
        padding: 0 5px;
        position: relative;
        .swiper-button-prev,
        .swiper-button-next {
          outline: none;
          width: 20px;
          height: 20px;
          margin-top: calc(-1 * 20px / 2 - 26px);
          border-radius: 50%;
          color: #b7bac1;
          &:after {
            font-size: 16px;
          }
        }
        .swiper-button-prev.swiper-button-disabled,
        .swiper-button-next.swiper-button-disabled {
          pointer-events: auto;
        }
        .swiper-button-prev {
          left: -10px;
          right: auto;
        }
        .swiper-button-next {
          right: -10px;
          left: auto;
        }
        .item {
          padding: 0 5px;
          .image {
            width: 100%;
            height: 105px;
          }
          .desc {
            margin-top: 8px;
          }
          .bottom {
            margin-top: 10px;
          }
        }
      }
    }

    // 直播
    &-live {
      .item {
        position: relative;
        .image {
          position: relative;
          .el-image {
            width: 100%;
            height: 100%;
            border-radius: 100%;
            ::v-deep.image-error {
              width: 100%;
              height: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              width: 100%;
              height: 100%;
              background-color: #f1f1f1;
              border-radius: 8px;
              .image-error__icon {
                font-size: 30px;
                color: #e0e0e0;
              }
            }
          }
          .tag {
            padding: 2px 5px;
            font-size: 12px;
            font-weight: 400;
            color: #ffffff;
            line-height: 14px;
            background: #67c23a;
            border-radius: 4px;
            position: absolute;
            bottom: -8px;
            left: 50%;
            transform: translateX(-50%);
            white-space: nowrap;
            .icon {
              width: 12px;
              margin-right: 2px;
            }
          }
        }
        .content {
          .title {
            display: flex;
            align-items: center;
            img {
              min-width: 0;
              margin-right: 3px;
            }
            .name {
              flex: 1;
              min-width: 0;
              font-size: 14px;
              font-weight: 500;
              color: #303133;
              line-height: 18px;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              -webkit-box-orient: vertical;
            }
          }
          .bottom {
            display: flex;
            align-items: center;
            .text {
              font-size: 12px;
              font-weight: 400;
              color: #909399;
              line-height: 17px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              max-width: 80px;
            }
            .line {
              width: 1px;
              height: 10px;
              background: #e4e7ed;
              margin: 0 10px;
            }
            .price {
              font-size: 15px;
              font-weight: 500;
              color: #f54030;
              line-height: 21px;
              margin-left: auto;
            }
            .free {
              font-size: 15px;
              font-weight: 500;
              color: #67c23a;
              line-height: 21px;
              margin-left: auto;
            }
          }
        }
        .time {
          font-size: 11px;
          font-weight: 400;
          color: #303133;
          line-height: 16px;
          white-space: nowrap;
        }
      }
      &.grid {
        display: grid;
        grid-gap: 14px 10px;
        &.grid-columns-2 {
          grid-template-columns: repeat(2, 1fr);
          .item {
            padding: 20px 15px;
            border-radius: 6px;
            border: 1px solid #e4e7ed;
            display: flex;
            align-items: center;
            .image {
              width: 86px;
              height: 86px;
            }
            .time {
              margin-top: 10px;
              color: #909399;
            }
            .content {
              flex: 1;
              min-width: 0;
              margin-left: 25px;
              .bottom {
                .text {
                  max-width: 80px;
                }
              }
            }
            .bottom {
              margin-top: 12px;
            }
          }
        }
        &.grid-columns-3 {
          grid-template-columns: repeat(3, 1fr);
          .item {
            padding: 30px 15px 20px;
            border-radius: 6px;
            border: 1px solid #e4e7ed;
            .image {
              width: 86px;
              height: 86px;
              margin: 0 auto;
            }
            .time {
              border-radius: 14px;
              position: absolute;
              bottom: -5px;
              left: 50%;
              transform: translateX(-50%);
              background: #f7f7f7;
              padding: 4px 9px;
            }
            .content {
              margin-top: 25px;
              .bottom {
                .text {
                  max-width: 80px;
                }
              }
            }
            .bottom {
              margin-top: 12px;
            }
          }
        }
        &.grid-columns-4 {
          grid-template-columns: repeat(4, 1fr);
          .item {
            padding: 30px 15px 20px;
            border-radius: 6px;
            border: 1px solid #e4e7ed;
            .image {
              width: 86px;
              height: 86px;
              margin: 0 auto;
            }
            .time {
              border-radius: 14px;
              position: absolute;
              bottom: -5px;
              left: 50%;
              transform: translateX(-50%);
              background: #f7f7f7;
              padding: 4px 9px;
            }
            .content {
              margin-top: 25px;
              .bottom {
                .text {
                  max-width: 60px;
                }
              }
            }
            .bottom {
              margin-top: 12px;
            }
          }
        }
      }
      &.scroll {
        position: relative;
        .swiper-button-prev,
        .swiper-button-next {
          outline: none;
          width: 20px;
          height: 20px;
          margin-top: 20px;
          border-radius: 50%;
          color: #b7bac1;
          &:after {
            font-size: 16px;
          }
        }
        .swiper-button-prev.swiper-button-disabled,
        .swiper-button-next.swiper-button-disabled {
          pointer-events: auto;
        }
        .swiper-button-prev {
          left: -10px;
          right: auto;
        }
        .swiper-button-next {
          right: -10px;
          left: auto;
        }
        .header {
          .title {
            font-size: 16px;
            font-weight: 500;
            color: #303133;
            line-height: 24px;
            text-align: center;
          }
          .line {
            margin-top: 20px;
            border-bottom: 1px solid #e4e7ed;
            position: relative;
          }
          .num {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            z-index: 999;
            font-size: 13px;
            font-weight: 400;
            color: #909399;
            background: #f4f4f5;
            border-radius: 11px;
            padding: 4px 15px;
          }
        }
        .item {
          display: flex;
          align-items: center;
          height: 160px;
          padding: 0 30px;
          margin: 36px 20px 0;
          border-radius: 6px;
          border: 1px solid #e4e7ed;
          .image {
            width: 86px;
            height: 86px;
          }
          .time {
            margin-top: 15px;
            color: #909399;
          }
          .content {
            flex: 1;
            min-width: 0;
            margin-left: 15px;
            .bottom {
              margin-top: 15px;
            }
          }
        }
      }
    }
  }
}
</style>
